import {Auth} from './auth'
import {mymessage} from '../../tools/mymessage'
import {postRequest} from '../../tools/api'

const defaultUser = {login: '', password: '', remember: false}

export default {
  state: {
    /** 用户登陆角色信息 */
    userInfo: Auth.getUserInfo() || {},
    /** 用户登陆账户密码 */
    accountRemember: Auth.getAccountRemember() || defaultUser,
    /** 是否登陆 */
    isLogin: Auth.getLogin() || false
  },
  /** 计算属性 */
  getters: {},
  /** 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation */
  mutations: {
    ACCOUNT_AUTH_STATUS_CHANGED (state, data) {
      const d = data.data
      // console.log('当前登录角色信息：', d)
      if (data.remember) {
        Auth.setAccountRemember(d)
      } else {
        state.userInfo = d

        state.isLogin = true
        Auth.setUserInfo(d)
        Auth.setLogin()
      }
    },
    ACCOUNT_LOGOUT (state) {
      state.userInfo = {}
      state.accountRemember = {}
      state.permission = []
      state.isLogin = false
      Auth.removeUserInfo()
      Auth.removeAccountRemember()
      Auth.removeLogin()
      mymessage.success('退出成功')
    }
  },
  /** Action 提交的是 mutation，而不是直接变更状态。Action 可以包含任意异步操作。 */
  actions: {
    /** 登录 */
    submitLogin ({commit}, params) {
      return new Promise((resolve, reject) => {
        postRequest('/user/login', params).then(resp => {
          // console.log("resp: ", resp);
          if (resp.code === 200) {
            commit('ACCOUNT_AUTH_STATUS_CHANGED', {...resp, ...params})
            mymessage.success('登录成功')
            resolve(resp)
          } else {
            sessionStorage.setItem('isLogin', false)
            mymessage.warning('账号或密码错误,请重新输入')
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('账号或密码错误,请重新输入')
          }
        }).catch(err => {
          reject(err)
          commit('ACCOUNT_LOGOUT')
        })
      })
    },
    submitSettingInfo ({commit}, params) {
      return new Promise((resolve, reject) => {
        postRequest('/user/update', params).then(resp => {
          console.log('resp', resp)
          if (resp.code === 200) {
            const data = params
            data.educationBgm = Number(data.educationBgm)
            commit('ACCOUNT_AUTH_STATUS_CHANGED', {...resp, data})
            mymessage.success('修改信息成功')
            resolve({...resp, data})
          } else {
            mymessage.warning('修改信息失败')
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('修改信息失败')
          }
        }).catch(err => {
          reject(err)
          commit('ACCOUNT_LOGOUT')
        })
      })
    },
    updatePassword ({commit}, params) {
      return new Promise((resolve, reject) => {
        postRequest('/user/updatePassword', params).then(resp => {
          // console.log('resp', resp)
          if (resp.code === 200) {
            mymessage.success('修改密码成功，请重新登录')
            resolve({...resp})
          } else {
            mymessage.warning('修改信息失败')
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('修改信息失败')
          }
        }).catch(err => {
          reject(err)
          commit('ACCOUNT_LOGOUT')
        })
      })
    },
    /** 登出 */
    accountLogoutSubmit ({commit}) {
      commit('ACCOUNT_LOGOUT')
      mymessage.success('退出成功')
    }
  }
}
